﻿using FairyGUI;

namespace GameFramework.UI.FairyGUI
{
    public static class FairyGUIComponentExtensions
    {
        /// <summary>
        /// 获取组件所属的界面实例
        /// </summary>
        public static IUIForm GetUIForm(this GComponent gComponent)
        {
            while (gComponent != null)
            {
                if (gComponent.data is IUIForm uiForm)
                    return uiForm;

                gComponent = gComponent.parent;
            }

            return null;
        }

        /// <summary>
        /// 获取组件所属的界面逻辑实例
        /// </summary>
        public static AFairyGUIFormLogic GetUIFormLogic(this GComponent gComponent)
        {
            var uiForm = gComponent.GetUIForm() as FairyGUIForm;
            if (uiForm == null)
                return null;
            
            return uiForm.Handle as AFairyGUIFormLogic;
        }
    }
}